package com.hhq.controller;

import com.hhq.entity.Dept;
import com.hhq.service.Deptservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class DeptController {
    @Autowired
    private Deptservice deptservice;

    @RequestMapping("/getAllDept")
    public String getAllDept(Model model){

        List<Dept> list= deptservice.getAllDept();
        model.addAttribute("requestScopelist", list);
        for (Dept dept : list)
            System.out.println(dept);
        return "dept/list";
    }

    @RequestMapping("/toAddDept")
    public String toAddDept(){
        return "dept/add";
    }

    @RequestMapping("/addDept")
    public  String addDept(Dept dept){
        int num = deptservice.addDept(dept);
        System.out.println(dept);
        System.out.println(num);
        return  "forward:/getAllDept";
    }

    @RequestMapping("/findDeptByName")
    public String findDeptByName(Model model, String name, HttpServletRequest request){
        List<Dept> list = deptservice.findDeptByName(name);
        System.out.println(list);
        if (list.size()!=0){
            model.addAttribute("requestScopelist",list);
            return "dept/list";
        }
        else {
            request.setAttribute("message","不存在该用户，请重新输入！");
            return "forward:/getAllDept";
        }
    }

    @RequestMapping("/deleteDept")
    public String deleteDept(String name){
        deptservice.deleteDept(name);
        System.out.println("Yes");
        return "forward:/getAllDept";
    }

}
